package com.zx.viewlearn.utils;

import android.util.Log;
import android.view.View;

/**
 * Created by zhoux on 2018/2/12.
 * 注释：
 */

public class ViewUtils {
    public static int getMySize(int measureSpec,int defalut) {
        int result = 0;
        int specMode = View.MeasureSpec.getMode(measureSpec);
        int specSize = View.MeasureSpec.getSize(measureSpec);
        Log.e("MyView3", "specMode:" + specMode);
        Log.e("MyView3", "specSize:" + specSize);//当没有给固定值时候(match_parent，wrap_content)，specSize给的是充满父控件的值。
        switch (specMode) {
            case View.MeasureSpec.EXACTLY://view设置成match_parent时候，会是这个模式.
                Log.e("MyView3", "MeasureSpec.EXACTLY:" + View.MeasureSpec.EXACTLY);//1073741824
                result = specSize;
                break;
            case View.MeasureSpec.AT_MOST://view设置成wrap_content时候，会是这个模式
                Log.e("MyView3", "MeasureSpec.AT_MOST:" + View.MeasureSpec.AT_MOST);//-2147483648
                result = defalut;
                result = Math.min(result, specSize);//设置成wrap_content时候，给一个默认值，这样就不会充满屏幕了
                break;
            case View.MeasureSpec.UNSPECIFIED:
                Log.e("MyView3", "MeasureSpec.UNSPECIFIED:" + View.MeasureSpec.UNSPECIFIED);//0
                result = specSize;
                break;
            default:
                break;
        }
        return result;

    }

}
